import json
from unittest import result
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import sqlite3
import typing

class BaseInfo(QWidget):
    def __init__(self) -> None:
        super().__init__()
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(242,242,242))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.setUI()
    def setUI(self):
        self.left = CourseInfo(self)
        self.right = Goal(self)
        self.right.move(300,0)
        self.left.move(0,0)
        self.btn = QPushButton(self)
        self.btn.setText("添加老师")
        self.btn.move(0,340)
        self.confirm_btn = QPushButton("确定",self)
        self.confirm_btn.move(230,340)
        self.confirm_btn.clicked.connect(self.add_classes)
        self.btn.clicked.connect(self.add_teacher)
    def add_classes(self):
        tmp = self.left
        class_name = tmp.line1.text()
        class_code = tmp.line2.text()
        class_type = tmp.line3.text()
        class_time = tmp.line4.text()
        class_score = tmp.line5.text()
        class_term = tmp.line6.text()
        class_object = tmp.line7.text()
        class_teacher = tmp.line8.text()
        class_goal = []
        class_goal_str = ""
        pointer = self.right
        for i in range(len(pointer.line_list)):
            class_goal.append(pointer.line_list[i].text())
        class_goal_str =  json.dumps(class_goal)
        course = Course()
        course.add_class([class_name,class_code,class_type,class_time,class_score,class_term,class_object,class_teacher,class_goal_str])
        QMessageBox.information(self,"添加成功","添加成功",QMessageBox.Yes)
    def add_teacher(self):
        self.teacher = AddTeacher()
        self.teacher.get_widget_pointer(self.left.line8)
        self.teacher.show()

class CourseInfo(QWidget):
    def __init__(self,parent=None) -> None:
        super().__init__(parent=parent)
        self.layout = QFormLayout()
        self.layout.setSpacing(20)
        self.line1 = QLineEdit()
        self.line2 = QLineEdit()
        self.line3 = QLineEdit()
        self.line4 = QLineEdit()
        self.line5 = QLineEdit()
        self.line6 = QLineEdit()
        self.line7 = QLineEdit()
        self.line8 = QLineEdit()
        self.line1.setFixedWidth(200)
        self.line2.setFixedWidth(200)
        self.line3.setFixedWidth(200)
        self.line4.setFixedWidth(200)
        self.line5.setFixedWidth(200)
        self.line6.setFixedWidth(200)
        self.line7.setFixedWidth(200)
        self.line8.setFixedWidth(200)
        self.setLayout(self.layout)

        self.layout.addRow("课程名称",self.line1)
        self.layout.addRow("课程编号",self.line2)
        self.layout.addRow("课程类别",self.line3)
        self.layout.addRow("课程学时",self.line4)
        self.layout.addRow("课程学分",self.line5)
        self.layout.addRow("开设学期",self.line6)
        self.layout.addRow("授课对象",self.line7)
        self.layout.addRow("任课老师",self.line8)
        self.line2.textEdited[str].connect(lambda: self.on_change())
    def on_change(self):
        classname = self.line1.text()
        classcode = self.line2.text()
        course = Course()
        if course.select_class(classname,classcode)!=None:
            QMessageBox.information(self,"课程已存在","课程"+classname+"已存在!",QMessageBox.Yes)
class Goal(QWidget):
    def __init__(self,parent=None) -> None:
        super().__init__(parent=parent)

        self.btn1 = QPushButton("添加目标",self)
        self.btn1.clicked.connect(self.add_goal)
        self.btn2 = QPushButton("删除目标",self)
        self.btn2.clicked.connect(self.del_goal)
        self.btn1.move(5,10)
        self.btn2.move(150,10)
        self.line_list = []
        for i in range(4):
            self.line_list.append(QLineEdit(self))
            self.line_list[-1].setFixedSize(245,65)
            self.line_list[-1].move(5,50+(len(self.line_list)-1)*70)
        self.resize(250,360)
    def add_goal(self):
        line = QEdit(self)
        line.setFixedSize(245,65)
        line.show()
        line.move(5,50+(len(self.line_list))*70)
        self.line_list.append(line)
    def del_goal(self):
        if len(self.line_list)>0:
            self.line_list[-1].close()
            del self.line_list[-1]
class Course():
    def __init__(self) -> None:
        try:
            self.conn = sqlite3.connect('class_manager.db')
        except:
            print("connect error")
    def add_class(self,values:typing.List):
        sql = "insert into class(classname,classcode,classtype,classhours,\
        classcredit,classterm,classobject,classteacher,classgoal)values("\
        +"'"+values[0]+"','"+values[1]+"','"+values[2]+"',"+values[3]+","\
        +values[4]+","+"'"+values[5]+"',"+"'"+values[6]+"',"+"'"+values[7]+"','"+values[8]+"')"
        cursor = self.conn.cursor()
        result = cursor.execute(sql)
        self.conn.commit()
        cursor.close()
    def dispaly_all_class(self):
        sql = "select * from class"
        cursor = self.conn.cursor()
        result = cursor.execute(sql)
        result =  result.fetchall()
        cursor.close()
        return result
    def select_class(self,classname,classcode):
        sql = "select * from class where classname=? and classcode=?"
        cursor = self.conn.cursor()
        result = cursor.execute(sql,(classname,classcode))
        result = result.fetchone()
        return result
    def delete_course(self,classname,classcode):
        sql = "delete from class where classname=? and classcode=?"
        cursor = self.conn.cursor()
        cursor.execute(sql, (classname,classcode))
        self.conn.commit()
        cursor.close()

class AddTeacher(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent=parent)
        self.setWindowTitle("教师管理")
        self.setGeometry(500,200,400,260)
        self.addbtn = QPushButton("新增",self)
        self.delbtn = QPushButton("删除",self)
        self.confirm_btn = QPushButton("确认",self)
        self.table = QTableWidget(self)
        self.delbtn.clicked.connect(self.del_table)
        self.addbtn.clicked.connect(self.add_table)
        self.confirm_btn.clicked.connect(self.confirm)
        self.table.setFixedWidth(400)
        self.table.setRowCount(6)
        self.table.setColumnCount(2)
        self.table.setColumnWidth(0,190)
        self.table.setColumnWidth(1,190)
        self.table.setHorizontalHeaderLabels(['姓名','手机号'])
        self.addbtn.move(0,190)
        self.addbtn.setFixedWidth(190)
        self.delbtn.move(200,190)
        self.delbtn.setFixedWidth(190)
        self.confirm_btn.move(0,220)
        self.confirm_btn.setFixedWidth(400)
    def get_widget_pointer(self,pointer:QLineEdit):
        self.pointer = pointer
    def confirm(self):
        count = self.table.rowCount()
        result = ""
        for i in range(count):
            try:
                name = self.table.item(i,0).text()
                phone = self.table.item(i,1).text()
                result+=name+","+phone+";"
            except:
                pass
        self.pointer.setText(result)
        self.close()
    def del_table(self):
        if self.table.rowCount()!=0:
            self.table.setRowCount(self.table.rowCount()-1)
    def add_table(self):
        self.table.setRowCount(self.table.rowCount()+1)
if __name__=="__main__":
    app = QApplication(sys.argv)
    window = BaseInfo()
    window.show()
    sys.exit(app.exec_())